package com.manxi.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manxi.constant.MessageConstant;
import com.manxi.constant.StatusConstant;
import com.manxi.context.BaseContext;
import com.manxi.dto.DishDTO;
import com.manxi.dto.DishPageQueryDTO;
import com.manxi.entity.Dish;
import com.manxi.entity.DishFlavor;
import com.manxi.entity.Setmeal;
import com.manxi.exception.DeletionNotAllowedException;
import com.manxi.mapper.DishFlavorMapper;
import com.manxi.mapper.DishMapper;
import com.manxi.mapper.SetmealDishMapper;
import com.manxi.result.PageResult;
import com.manxi.service.DishService;
import com.manxi.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setCount(0);
        //向菜品表插入一条数据
        dishMapper.insert(dish);

        //获取刚刚新增菜品的id
        Long dishId = dish.getId();

        //获取菜品口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });

            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    public DishVO getById(Long id) {
        //根据菜品id查询菜品信息
        Dish dish = dishMapper.getById(id);

        //根据菜品id查询菜品口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

        //将查询到的口味数据封装到Dish类中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 批量删除菜品和删除单个菜品
     *
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        //可以一次删除一个菜品，也可以批量删除多个菜品

        //判断当前菜品是否能够删除---是否存在起售中的菜品
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            //起售中的菜品不能删除
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断当前菜品是否能够删除---是否被套餐关联了
        List<Long> setmealsIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        //当前菜品被关联，不能删除
        if (setmealsIds != null && setmealsIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品后，与菜品关联的口味表也需要删除
        for (Long id : ids) {
            //删除菜品表中的菜品
            dishMapper.deleteById(id);

            //删除菜品关联的口味
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    /**
     * 更改菜品信息
     *
     * @param dishDTO
     */
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品信息
        dishMapper.update(dish);

        //先删除菜品相关联的口味表信息
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        //往口味表中插入新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 启售禁售菜品
     *
     * @param status
     * @param id
     */
    public void StartOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        //dishMapper.StartOrStop(status,id);
        dishMapper.update(dish);
    }

    /**
     * 套餐查询菜品列表
     *
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId, String name) {
        Dish dish = Dish.builder()
                .name(name)
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 根据分类id查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        //查询菜品
        List<Dish> dishList = dishMapper.list(dish);

        //创建DishVO集合
        List<DishVO> dishVOList = new ArrayList<>();

        //判断菜品集合是否为空，不为空则继续执行代码
        if (dishList != null && dishList.size() > 0) {
            //遍历菜品集合
            for (Dish d : dishList) {
                //创建DishVO
                DishVO dishVO = new DishVO();
                //拷贝Dish对象到DishVO
                BeanUtils.copyProperties(d, dishVO);
                //根据菜品id查询菜品对应的口味
                List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(d.getId());
                //设置菜品口味列表
                dishVO.setFlavors(dishFlavors);
                //添加到集合中
                dishVOList.add(dishVO);
            }
            return dishVOList;
        }
        return null;
    }
}
